package com.scpcfe.www.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
public class uploadFileDemo {
    @PostMapping("load_file")
    public <SimpleDataFormat> String enter(MultipartFile uploadfile, HttpServletRequest request) {


        if (uploadfile == null) {
            return "收到的是空文件";
        }
        System.out.println(uploadfile.getSize());
        try {
            SimpleDateFormat  sdf=  new SimpleDateFormat("yyyMMddHHmmss");
            String strData=sdf.format(new Date());
            System.out.println(strData);
            uploadfile.transferTo(new File("D:/" + UUID.randomUUID() + ".jpg"));//路径
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "收到了上传的文件";
    }

    @PostMapping("/upload_files")
    public String enters(MultipartFile[] uploadfiles, HttpServletRequest request) {
        if (uploadfiles == null || uploadfiles.length == 0) {
            return "你没有传文件";
        }
        System.out.println("你上传了" + uploadfiles.length + "个文件");
        for (int i = 0; i < uploadfiles.length; i++) {
            ;
            enter(uploadfiles[i], request);
        }
        return "收到了上传的文件";
    }
}





